layui.define(function (exports) {
  var $ = layui.$,
    admin = layui.admin;
  
  function initPreview(text) {
    var content =
      '<div class="layui-row layui-col-space40 LAY_custom_layer_row">' +
      '<div class="layui-col-md12 LAY_custom_layer_col">' +
      '<div class="LAY_custom_layer_title">商城链接</div>' +
      '<div class="LAY_custom_layer_code"></div>' +
      '<button type="button" class="layui-btn LAY_custom_layer_copy" data-clipboard-text="'+ text +'">' +
      '<i class="layui-icon layui-icon-unlink"></i> 复制链接' +
      '</button>' +
      '</div>' +
      '</div>';

    $('.LAY_btn_preview').off('click').on('click', function () {
      layer.open({
        title: '保险商城',
        type: 1,
        area: admin.screen() < 2 ? ['80%', '300px'] : ['600px', '500px'],
        shadeClose: true,
        content: content,
        success: function () {
          initLinkLayerEvents(text);
        }
      });
    });
  }

  //复制文本
  function copyUrl(target) {
    var clipboard = new Clipboard(target);
    layer.msg("已复制好，可贴粘。");
  }

  //初始化链接弹层内事件
  function initLinkLayerEvents(text) {
    // 二维码
    $('.LAY_custom_layer_code').qrcode({
      render: 'canvas',
      width: 150,
      height: 150,
      text: text
    });

    // 复制链接
    $('.LAY_custom_layer_copy').off('click').on('click', function () {
      copyUrl('.LAY_custom_layer_copy');
    });
  }

  exports('preview', {
    init: initPreview
  });
});